package uml.objects;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.TextField;

/**
 * @author Rick Myers
 */
public class ArrowEnd implements Translatable {

	private Point m_topLeft;
	private Polygon m_endType;
	private boolean m_filled;
	private TextField m_text;
	private Color m_color;

	/**
	 * This will create a basic <tt>ArrowEnd</tt> object.
	 */
	public ArrowEnd()
	{
		m_topLeft = new Point();
		m_filled = false;
		m_color = Color.black;
	}
	
	/**
	 * This will make the <tt>ArrowHead</tt> object paint itself.
	 */
	public void paint(Graphics g) {
		// At the least, we will always set the color of arrow head and draw it.
		g.translate(m_topLeft.x, m_topLeft.y);
		g.setColor(Color.black);
		g.drawPolygon(m_endType);

		// If the arrow head needs to be filled, we fill it. If not, we leave it
		// empty.
		if (m_filled)
			g.fillPolygon(m_endType);
	}

	/**
	 * Fills <tt>ArrowHead</tt> with specified m_color.
	 */
	public void setFilled() {
		m_filled = true;
	}

	/**
	 * Fills <tt>ArrowHead</tt> so that it is completely black.
	 */
	public boolean getFilled() {
		return m_filled;
	}

	/**
	 * This is used to indicate what type of polygon will be used as the end
	 * type. This function is used to change that polygon.
	 * 
	 * @param p
	 */
	public void setEndType(Polygon p) {
		m_endType = new Polygon();
		m_endType = p;
	}
	
	/**
	 * This is used to return the type of arrow head that is assigned to this 
	 * ArrowEnd object.
	 * @return m_endType Polygon.
	 */
	public Polygon getEndTypePolygon()
	{
		return m_endType;
	}

	/**
	 * This rotates the polygon at the end of the line so that it aligns
	 * according to how the class box is positioned.
	 * 
	 * @param angle (must be in radians)
	 */
	public void rotate(float angle) {
		
		int[] xCoords = m_endType.xpoints;
		int[] yCoords = m_endType.ypoints;

		// npoints is the number of valid points in a Polygon. This loop walks through all valid
		// coordinates and rotates them accordingly
		for(int x = 0; x < m_endType.npoints; x++)
		{
			m_endType.xpoints[x] = (int) (xCoords[x] * Math.cos(angle) - yCoords[x] * Math.sin(angle));
			m_endType.ypoints[x] = (int) (xCoords[x] * Math.sin(angle) + yCoords[x] * Math.cos(angle));
		}
		
	}

	/**
	 * Assigns text to an endType to show what type of relationship is being
	 * made. Example: <<Uses>>
	 * 
	 * @param s
	 */
	public void setText(String s) {
		m_text = new TextField(s);
	}
	
	/**
	 * This will assign the location of the arrow end object.
	 * 
	 * @return m_topLeft
	 */
	public void setLocation(int x, int y)
	{
		m_topLeft = new Point(x,y);
	}
	
	/**
	 * This will assign the location of the arrow end object.
	 * 
	 * @return m_topLeft
	 */
	public Point getLocation()
	{
		return m_topLeft;
	}
	
	/**
	 * Returns the m_Text object that specifies the string which describes the type of 
	 * relationship.
	 * 
	 * @return m_Text object.
	 */
	public TextField getText() {
		return m_text;
	}

	/**
	 * This function overrides the translatable select function. It is used to test if a 
	 * point that is clicked on is the m_topLeft point of this object.
	 */
	@Override
	public boolean select(Point p) {
		if (m_topLeft.equals(p))
			return true;
		else
			return false;
	}

	/**
	 * This will translate the arrow head to the specified location.
	 */
	@Override
	public void translate(int x, int y) {
		m_topLeft.x += x;
		m_topLeft.y += y;
	}

	/**
	 * This will set the color of the arrow head. Calling the fill function will fill in the 
	 * arrow head with the specified color
	 * @param color
	 */
	public void setColor(Color c) {
		m_color = new Color(0);
		m_color = c;
	}
	
	/**
	 * This will return the color used to paint the object.
	 * @param color
	 */
	public Color getColor() {
		return m_color;
	}
}


/*// This is the long way of rotating
//======================================================================
// Assuming angle is not in radians.
angle = (float) Math.toRadians(angle);

// Creating arrays to host old coordinates before we change them
int[] xCoords = m_endType.xpoints;
int[] yCoords = m_endType.ypoints;

// npoints is the number of valid points in a Polygon. This loop walks through all valid
// coordinates and rotates them accordingly
for(int x = 0; x < m_endType.npoints; x++)
{
	m_endType.xpoints[x] = (int) (xCoords[x] * Math.cos(angle) - yCoords[x] * Math.sin(angle));
	m_endType.ypoints[x] = (int) (xCoords[x] * Math.sin(angle) + yCoords[x] * Math.cos(angle));
}
//====================================================================== */